//
//  Tile.m
//  IssuesGame
//
//  Created by Selah Ben-Haim on 10/13/09.
//  Copyright 2009 Regents of the University of Minnesota
//

#import "Tile.h"


@implementation Tile

@synthesize perspective, status, question, answer, imagePath, answers, isQuestionTile;

- (id)initWithTileDictionary:(NSMutableDictionary *)tileDictionary andPerspectiveDictionary:(NSDictionary *)perspectiveDictionary
{
	if (self = [super init]) {

		dict        = [tileDictionary retain];
		perspective = [[tileDictionary valueForKey:kTilePerspective] retain];
		
		if ( [perspective isEqualToString:kTilePerspectiveBlank] ) {
			status      = nil;
			question    = nil;
			answer      = -1;
			imagePath   = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/tile_empty.png"];
			answers     = nil;
			isQuestionTile = NO;
		}
		else if ( [perspective isEqualToString:kTilePerspectiveStart] ) {
			status      = nil;
			question    = nil;
			answer      = -1;
			imagePath   = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/tile_start.png"];
			answers     = nil;
			isQuestionTile = NO;
		}
		else if ( [perspective isEqualToString:kTilePerspectiveFinish] ) {
			status      = nil;
			question    = nil;
			answer      = -1;
			imagePath   = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/tile_finish.png"];
			answers     = nil;
			isQuestionTile = NO;
		}
		else {
			status      = [[tileDictionary valueForKey:kTileStatus] retain];
			question    = [[tileDictionary valueForKey:kTileQuestion] retain];
			answer      = [[tileDictionary valueForKey:kTileAnswer] intValue];

			NSString *nameWithPrefix = [@"gamedocs/" stringByAppendingString:[perspectiveDictionary valueForKey:kPerspectiveImagePath]];
			imagePath   = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:nameWithPrefix];

			NSMutableArray *allAnswers = [NSMutableArray arrayWithArray:[perspectiveDictionary valueForKey:kPerspectiveAnswers]];
			
			answers = [NSMutableArray new];
			
			NSString *answerString = [allAnswers objectAtIndex:answer];
			[allAnswers removeObjectAtIndex:answer];
			
			while ([answers count] < kGameNumberOfAnswerOptions - 1 && [allAnswers count] > 0) {
				int index = arc4random() % [allAnswers count];
				[answers addObject:[allAnswers objectAtIndex:index]];
				[allAnswers removeObjectAtIndex:index];
			}
			
			int randomIndex = arc4random() % ([answers count] + 1);
			[answers insertObject:answerString atIndex:randomIndex];
			answer = randomIndex;
			isQuestionTile = YES;
		}
	}

	return self;
}

- (void)correctlyAnswered
{
	[dict setObject:kTileStatusAnsweredCorrectly forKey: kTileStatus];
	[dict setObject:kTilePerspectiveBlank forKey:kTilePerspective];
	status      = kTileStatusAnsweredCorrectly;
	question    = nil;
	answer      = -1;
	imagePath   = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/tile_empty.png"];
	answers     = nil;
	isQuestionTile = NO;
}


- (void)incorrectlyAnswered
{
	[dict setObject:kTileStatusAnsweredIncorrectly forKey: kTileStatus];
	[dict setObject:kTilePerspectiveBlank forKey:kTilePerspective];
	status      = kTileStatusAnsweredIncorrectly;
	question    = nil;
	answer      = -1;
	imagePath   = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/tile_empty.png"];
	answers     = nil;
	isQuestionTile = NO;
}


- (void)resetStatus
{
	[dict setObject: kTileStatusUnanswered forKey: kTileStatus];
}

@end

